/**
 * Created by Administrator on 2017/3/13.
 */
$(document).ready(function(){
    $("#username").blur(function () {
        var username = $("#username").val();
        $(".login-tip").hide();
        if(username == ""){
            $(".name-tip").text("账号不能为空！请输入账号");
            $(".name-tip").css("color","#FF3030");
        }else {
            $(".name-tip").text("请输入账号");
            $(".name-tip").css("color","#696969");
        }
    });
    $("#password").blur(function () {
        var password = $("#password").val();
        $(".login-tip").hide();
        if(password == ""){
            $(".password-tip").text("密码不能为空！请输入账号");
            $(".password-tip").css("color","#FF3030");
        }else {
            $(".password-tip").text("请输入密码");
            $(".password-tip").css("color","#696969");
        }
    });
        $("#login_button").on("click",function () {
            var username = $("#username").val();
            var password = $("#password").val();
            if(username == ""){
                $(".name-tip").text("请输入账号进行登录");
                $(".name-tip").css("color","#FF3030");
            }else if(password == ""){
                $(".password-tip").text("请输入密码进行登录");
                $(".password-tip").css("color","#FF3030");
            }else{
                $(".name-tip").text("请输入账号");
                $(".name-tip").css("color","#696969");
                $(".password-tip").text("请输入密码");
                $(".password-tip").css("color","#696969");
                $.ajax({
                    url:"/login/check.action",
                    type:"post",
                    dataType:"json",
                    data:{
                        username:$("#username").val(),
                        password:$("#password").val()
                    },
                    success:function (data) {
                        if(data.checkResult == "user_not_exist"){
                            var tip = $(".login-tip");
                            $(tip,"span").text("用户不存在！请重新输入");
                            $(tip,"span").css("color","#FF3030");
                            $("#username").val("");
                            tip.show();
                        }else if(data.checkResult == "password_wrong"){
                            var tip = $(".login-tip");
                            $(tip,"span").text("密码错误！请重新输入");
                            $(tip,"span").css("color","#FF3030");
                            $("#password").val("");
                            tip.show();
                        }else if(data.checkResult == "login_succeed"){
                            window.location.href = "/login/login.action?name="+encodeURI(encodeURI(username))+"&item=home";
                        }
                    },
                    error:function (e) {
                        alert("请求登录失败："+e);
                    }
                });
            }
        });
});